<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(n, k) {
      let ret = []
      const helper = (start, prev) => {
        for (let i = start; i <= n; i++) { // 第一层
          const cur = prev.concat(i)
          if (cur.length === k) { // 出口条件
            ret.push(cur)
          } else {
            helper(i + 1, cur) // 第二层
          }
        }
      }
      helper(1, [])
      return ret
    };
    console.log(arr(4, 2));
  </script>
</body>

</html>